import { Controller, Get, Query } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

import { ApiResult } from '~/common/decorators/api-result.decorator';
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator';
import { Pagination } from '~/helper/paginate/pagination';
import {
  definePermission,
  Perm,
} from '~/modules/auth/decorators/permission.decorator';

import {
  CaptchaLogQueryDto,
  LoginLogQueryDto,
  TaskLogQueryDto,
  operationLogQueryDto,
} from './dto/log.dto';
import { CaptchaLogEntity } from './entities/captcha-log.entity';
import { TaskLogEntity } from './entities/task-log.entity';
import { OperationLogEntity } from './entities/operation-log.entity';
import { LoginLogInfo, OperationLogInfo } from './models/log.model';
import { CaptchaLogService } from './services/captcha-log.service';
import { LoginLogService } from './services/login-log.service';
import { TaskLogService } from './services/task-log.service';
import { OperationLogService } from './services/operation-log.service';
import { Public } from '../../auth/decorators/public.decorator';

export const permissions = definePermission('system:log', {
  TaskList: 'task:list',
  LogList: 'login:list',
  CaptchaList: 'captcha:list',
  OperationList: 'operation:list',
} as const);

@ApiSecurityAuth()
@ApiTags('System - 日志模块')
@Controller('log')
export class LogController {
  constructor(
    private loginLogService: LoginLogService,
    private taskService: TaskLogService,
    private captchaLogService: CaptchaLogService,
    private operationLogService: OperationLogService
  ) {}

  @Get('login/list')
  @Public()
  @ApiOperation({ summary: '查询登录日志列表' })
  @ApiResult({ type: [LoginLogInfo], isPage: true })
  @Perm(permissions.TaskList)
  async loginLogPage(
    @Query() dto: LoginLogQueryDto
  ): Promise<Pagination<LoginLogInfo>> {
    return this.loginLogService.list(dto);
  }

  @Get('task/list')
  @ApiOperation({ summary: '查询任务日志列表' })
  @ApiResult({ type: [TaskLogEntity], isPage: true })
  @Perm(permissions.LogList)
  async taskList(@Query() dto: TaskLogQueryDto) {
    return this.taskService.list(dto);
  }

  @Get('captcha/list')
  @ApiOperation({ summary: '查询验证码日志列表' })
  @ApiResult({ type: [CaptchaLogEntity], isPage: true })
  @Perm(permissions.CaptchaList)
  async captchaList(
    @Query() dto: CaptchaLogQueryDto
  ): Promise<Pagination<CaptchaLogEntity>> {
    return this.captchaLogService.paginate(dto);
  }

  @Get('operation/list')
  @Public()
  @ApiOperation({ summary: '查询操作日志列表' })
  @ApiResult({ type: [OperationLogInfo], isPage: true })
  @Perm(permissions.OperationList)
  async operationList(
    @Query() dto: operationLogQueryDto
  ): Promise<Pagination<OperationLogInfo>> {
    return this.operationLogService.list(dto);
  }
}
